二进制问题

题目 二进制问题

image-0ac2fe54

思路分析

发现又可以暴力枚举

image-f420ba35

先把暴力的写了 后面再说

正解是数位dp 不管 暴力直接过六个

代码实现

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'

int n,k;
int lowbit(int x){
	return x&-x;
}

int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>k;
	int res=0;
	for(int i=1;i<=n;i++){
		int x=i;
		int cnt=0;
		while(x){
			x-=lowbit(x);
			cnt++;
		}
		if(cnt==k)
			res++;
	}
	cout<<res;
	return 0;
}

同类题型

视频讲解


⬅️ 异或变换 🏠 00-冲刺国赛 ➡️ 翻转括号序列